package com.tj.collection.db.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kly.user.service.UserService;
import com.tj.collection.controller.chatter.request.AddChatterGoalRequest;
import com.tj.collection.db.dao.AdminChatterDao;
import com.tj.collection.db.dao.ChatterWorkSettingDao;
import com.tj.collection.db.dao.ChatterConfigDao;
import com.tj.collection.db.dao.RptModelLikeDao;
import com.tj.collection.db.domain.ChatterGoalDto;
import com.tj.collection.db.entity.AdminChatterEntity;
import com.tj.collection.db.entity.ChatterWorkSettingEntity;
import com.tj.collection.db.entity.ChatterConfigEntity;
import com.tj.collection.db.entity.RptModelLikeEntity;
import com.tj.collection.db.service.ChatterOptService;
import com.tj.collection.enums.ChatterConfigTypeEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChatterOptServiceImpl implements ChatterOptService {
    @Resource
    AdminChatterDao adminChatterDao;
    @Resource
    UserService userService;
    @Resource
    ChatterConfigDao chatterConfigDao;
    @Resource
    ChatterWorkSettingDao chatterWorkSettingDao;
    @Resource
    RptModelLikeDao modelLikeDao;
    /**
     * 单位：小时
     */
    private static final Integer unit_per_hour = 1;
    /**
     * 单位：天
     */
    private static final Integer unit_per_day = 2;

    @Override
    public void addChatterAdmin(String chatterAccount, Long adminId) {
        final String chatterCode = userService.getUserCodeByAccount(chatterAccount);
        if (StringUtils.isEmpty(chatterCode)) {
            return;
        }
        final List<AdminChatterEntity> adminChatterEntities = adminChatterDao.listAdminChatterEntity(adminId);
        if (CollectionUtils.isNotEmpty(adminChatterEntities)) {
            final boolean isExists = adminChatterEntities.parallelStream().anyMatch(o -> o.getChatterCode().equals(chatterAccount));
            if (!isExists) {
                addChatterAdmin(adminId, chatterCode);
            }
        } else {
            addChatterAdmin(adminId, chatterCode);
        }
    }


    @Override
    public List<Long> listAllAdminIds() {

        final List<AdminChatterEntity> adminChatterEntities = adminChatterDao.listAllAdminChatterEntity();
        if (CollectionUtils.isEmpty(adminChatterEntities)) {
            return Lists.newArrayList();
        }
        return adminChatterEntities.parallelStream().map(AdminChatterEntity::getAdminId).distinct().map(Long::valueOf).collect(Collectors.toList());
    }

    @Override
    public List<Long> listAdminIds(Long adminId) {
        final List<AdminChatterEntity> adminChatterEntities = adminChatterDao.listAdminChatterEntity(adminId);
        if (CollectionUtils.isEmpty(adminChatterEntities)) {
            return Lists.newArrayList();
        }
        return adminChatterEntities.parallelStream().map(AdminChatterEntity::getAdminId).distinct().map(Long::valueOf).collect(Collectors.toList());
    }

    @Override
    public ChatterGoalDto getChatterGoal(String chatterCode) {
        final List<ChatterConfigEntity> chatterConfigEntities = chatterConfigDao.listChatterConfigs(chatterCode);
        final ChatterGoalDto dto = ChatterGoalDto.builder().chatterCode(chatterCode).onlineUserNum(0).breakIceChatNum(0).newRelationNum(0).totalPPVNum(0).build();
        if (CollectionUtils.isEmpty(chatterConfigEntities)) {
            return dto;
        }
        for (ChatterConfigEntity o : chatterConfigEntities) {
            if (ChatterConfigTypeEnum.ONLINE_USER_PRE_HOUR.getType().equals(o.getType())) {
                dto.setOnlineUserNum(o.getGoalValue());
            }
            if (ChatterConfigTypeEnum.NEW_RELATION_PRE_DAY.getType().equals(o.getType())) {
                dto.setNewRelationNum(o.getGoalValue());
            }
            if (ChatterConfigTypeEnum.BREAK_ICE_CHAT_PRE_DAY.getType().equals(o.getType())) {
                dto.setBreakIceChatNum(o.getGoalValue());
            }
            if (ChatterConfigTypeEnum.TOTAL_PPV_PRE_DAY.getType().equals(o.getType())) {
                dto.setTotalPPVNum(o.getGoalValue());
            }
        }

        return dto;
    }

    @Override
    public void modifyChatterWorkingHour(String chatterCode, Integer startHour1, Integer endHour1, Integer startHour2, Integer endHour2) {
        ChatterWorkSettingEntity chatterWorkingSet = chatterWorkSettingDao.getChatterWorkingSet(chatterCode);
        if (chatterWorkingSet == null) {
            chatterWorkingSet = new ChatterWorkSettingEntity();
            chatterWorkingSet.setChatterCode(chatterCode);
            setupWorkingHour(startHour1, endHour1, startHour2, endHour2, chatterWorkingSet);
            chatterWorkSettingDao.insert(chatterWorkingSet);
            return;
        }
        setupWorkingHour(startHour1, endHour1, startHour2, endHour2, chatterWorkingSet);
        chatterWorkSettingDao.updateByPrimaryKey(chatterWorkingSet);

    }

    private void setupWorkingHour(Integer startHour1, Integer endHour1, Integer startHour2, Integer endHour2, ChatterWorkSettingEntity chatterWorkingSet) {
        chatterWorkingSet.setStartHour(startHour1);
        chatterWorkingSet.setEndHour(endHour1);
        chatterWorkingSet.setStartHour2(startHour2);
        chatterWorkingSet.setEndHour2(endHour2);
        chatterWorkingSet.setTransferFlag(0);
        chatterWorkingSet.setStatus(1);
        // 第二天生效
        chatterWorkingSet.setEffectiveStartDate(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(), 1)));
        chatterWorkingSet.setEffectiveEndDate(DateUtil.beginOfDay(DateUtil.offset(DateUtil.date(), DateField.YEAR, 10)));
    }


    @Override
    public void addChatterGoal(AddChatterGoalRequest addChatterGoalReq) {
        String chatterCode = addChatterGoalReq.getChatterCode();
        final List<ChatterConfigEntity> chatterConfigEntities = chatterConfigDao.listChatterConfigs(chatterCode);
        if (CollectionUtils.isNotEmpty(chatterConfigEntities)) {
            chatterConfigDao.updateChatterConfig(chatterCode);
        }

        // 添加记录
        addChatterGoal(chatterCode, ChatterConfigTypeEnum.ONLINE_USER_PRE_HOUR.getType(), addChatterGoalReq.getOnlineUserNum(), unit_per_hour);
        addChatterGoal(chatterCode, ChatterConfigTypeEnum.NEW_RELATION_PRE_DAY.getType(), addChatterGoalReq.getNewRelationNum(), unit_per_day);
        addChatterGoal(chatterCode, ChatterConfigTypeEnum.BREAK_ICE_CHAT_PRE_DAY.getType(), addChatterGoalReq.getBreakIceChatNum(), unit_per_day);
        addChatterGoal(chatterCode, ChatterConfigTypeEnum.TOTAL_PPV_PRE_DAY.getType(), addChatterGoalReq.getTotalPPVNum(), unit_per_day);
    }

    private void addChatterGoal(String chatterCode, Integer type, Integer chatterGoalValue, Integer unit) {
        ChatterConfigEntity entity = new ChatterConfigEntity();
        entity.setChatterCode(chatterCode);
        entity.setStatus(1);
        entity.setType(type);
        entity.setUnitType(unit);
        entity.setGoalValue(chatterGoalValue);
        chatterConfigDao.insert(entity);
    }


    private void addChatterAdmin(Long adminId, String chatterCode) {
        final AdminChatterEntity adminChatterEntity = new AdminChatterEntity();
        adminChatterEntity.setAdminId(Integer.valueOf(String.valueOf(adminId)));
        adminChatterEntity.setChatterCode(chatterCode);
        adminChatterDao.insert(adminChatterEntity);
    }

    @Override
    public Map<String, String> modelLikeRate(List<String> modelCodes) {

        final Map<String, RptModelLikeEntity> modelDatas = modelLikeDao.listModelData(modelCodes);
        if (CollectionUtils.isEmpty(modelDatas)) {
            return Maps.newHashMap();
        }

        Map<String, String> retMap = Maps.newHashMap();
        modelDatas.forEach((k, v) -> {
            final Integer likeNum = v.getLikeNum();
            final Integer totalNum = v.getTotalNum() == null || v.getTotalNum() == 0 ? 1 : v.getTotalNum();
            retMap.put(k, String.format("%.2f", (double) likeNum * 100 / (double) totalNum) + "%");
        });


        return retMap;
    }
}
